package com.buba.personnel.controller;

import com.alibaba.fastjson.JSON;
import com.buba.personnel.pojo.department;
import com.buba.personnel.service.departmentService;
import com.buba.personnel.service.impl.departmentServiceImpl;
import com.buba.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/departmentServlet")
public class departmentServlet extends BaseServlet {
    departmentService departmentService =  new departmentServiceImpl();

    @Override
    public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag = request.getParameter("flag");
        if ("selDep".equals(flag))
            this.selDep(request,response);
        if ("addDep".equals(flag))
            this.addDep(request,response);
        if ("updDep".equals(flag))
            this.updDep(request,response);
        if ("delDep".equals(flag))
            this.delDep(request,response);
    }

    /**
     * 删除方法
     * @param request
     * @param response
     */
    private void delDep(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("删除连接");
        Map<String, String> param = getParam(request);
        String departId = request.getParameter("departId");
        int count = departmentService.delDep(departId);
        //返回数据
        response.getWriter().write(count+"");

    }

    /**
     * 修改
     * @param request
     * @param response
     */
    private void updDep(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("连接成功");
        //获取要修改的数据
        Map<String, String> param = getParam(request);
//        for(String key:param.keySet()){
//            System.out.println("key:"+key+" "+"Value:"+param.get(key));
//        }
        //传递业务层
        int count = departmentService.upddepartment(param);
        //返回数据
        response.getWriter().write(count+"");
    }

    /**
     * 添加方法
     * @param request
     * @param response
     */
    private void addDep(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取添加的表单数据，使用工具类（BaseServlet）获取表单信息
        Map<String, String> param = getParam(request);
        //将添加的数据传入业务层
        departmentService.insert(param);
        response.sendRedirect("buba/personnel/department.html");
    }

    /**
     * 查询方法
     * @param request
     * @param response
     * @throws IOException
     */
    private void selDep(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> param = getParam(request);
        // 调用业务层查询部门方法，返回部门集合
        List<department> list = departmentService.selDep(param);
        //用于装返回给浏览器的数据
        Map<String,Object> map = new HashMap();
        map.put("list",list);
        //添加当前页数
        map.put("page",Integer.parseInt(param.get("page")));
        //数据库总语句  查询总条数的方法
        long count = departmentService.selCount(param);
        //每页显示多少条
        int limit = Integer.parseInt(param.get("limit"));
        //总共有多少页 数据库的总数据 / 每页显示的条数
        long total = 0;
        if (count % limit == 0){
            total = count / limit;
        }else{
            total = (count / limit)+1;
        }
        map.put("total",total);
        response.getWriter().write(JSON.toJSONString(map));
    }

}
